package cn.tedu.jxc.controller;

import cn.tedu.jxc.pojo.dto.CategoryDTO;
import cn.tedu.jxc.pojo.vo.CategoryListItemVO;
import cn.tedu.jxc.pojo.vo.CategoryStandardVO;
import cn.tedu.jxc.pojo.vo.CategoryTreeListVO;
import cn.tedu.jxc.pojo.vo.ProductListItemVO;
import cn.tedu.jxc.service.CategoryService;
import cn.tedu.jxc.service.ProductService;
import cn.tedu.jxc.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户管理
 */

@Slf4j
@Validated
@RequestMapping("/categories")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品列表
     *
     * @return
     */
   @GetMapping("")
    public JsonResult<List<CategoryTreeListVO>> categoryTreeList() {
       List<CategoryTreeListVO> list = categoryService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/tree-list")
    public JsonResult<List<CategoryTreeListVO>> categoryTreeListByParenId(Long parentId) {
        List<CategoryTreeListVO> list = categoryService.treeList(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/list")
    public JsonResult<List<CategoryListItemVO>> getList(Long parentId) {
        List<CategoryListItemVO> list = categoryService.getList(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/{id:[\\d]+}")
    public JsonResult<CategoryStandardVO> getById(@Range(min = 1) @PathVariable Long id) {
        CategoryStandardVO categoryStandardVO = categoryService.getById(id);
        return JsonResult.ok(categoryStandardVO);
    }

    @PostMapping("/add-new")
    public JsonResult<Void> getById(CategoryDTO categoryDTO) {
        categoryService.addNew(categoryDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[\\d]+}/enable")
    public JsonResult<Void> enable(@PathVariable Long id){
        categoryService.enable(id);
       return JsonResult.ok();
    }

    @PostMapping("/{id:[\\d]+}/disable")
    public JsonResult<Void> disable(@PathVariable Long id){
        categoryService.disable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[\\d]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1) @PathVariable Long id){
        categoryService.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("/search")
    public JsonResult<List<CategoryListItemVO>> search(@NotNull(message = "查询内容不能为空")
                                                           @NotEmpty(message = "查询内容不能为空")
                                                                   String categoryName){
        List<CategoryListItemVO> list = categoryService.search(categoryName);
        log.debug("输出查询到的数据");
        return JsonResult.ok(list);
    }
}
